<template>
  <Dropdown placement="bottomLeft" :overlayClassName="`${prefixCls}-dropdown-overlay`">
    <span :class="[prefixCls, `${prefixCls}--${theme}`]" class="flex">
      <img :class="`${prefixCls}__header`" :src="getAvatarUrl" />
      <span :class="`${prefixCls}__info hidden md:block`">
        <span :class="`${prefixCls}__name  `" class="truncate">
          {{ getUserInfo.userName }}
        </span>
      </span>
    </span>

    <template #overlay>
      <Menu @click="handleMenuClick">
        <MenuItem key="doc" :text="t('layout.header.dropdownItemDoc')" icon="ion:document-text-outline" v-if="getShowDoc" />
        <MenuDivider v-if="getShowDoc" />
        <MenuItem key="account" :text="t('layout.header.dropdownItemSwitchAccount')" icon="ant-design:setting-outlined" />
        <!-- <MenuItem key="password" :text="t('layout.header.dropdownItemSwitchPassword')" icon="ant-design:edit-outlined" /> -->
        <MenuItem key="workspace" :text="t('layout.header.dropdownItemSwitchWorkspace')" icon="ant-design:cluster-outlined" />
        <MenuItem key="cache" :text="t('layout.header.dropdownItemRefreshCache')" icon="ion:sync-outline" />
        <!-- <MenuItem
            v-if="getUseLockPage"
            key="lock"
            :text="t('layout.header.tooltipLock')"
            icon="ion:lock-closed-outline"
        />-->
        <MenuItem key="logout" :text="t('layout.header.dropdownItemLoginOut')" icon="ion:power-outline" />
      </Menu>
    </template>
  </Dropdown>
  <LockAction @register="register" />
  <TenantSelect ref="loginSelectRef" />
  <UpdatePassword ref="updatePasswordRef" />
</template>
<script lang="ts">
  // components
  import { Dropdown, Menu } from "ant-design-vue"

  import { defineComponent, computed, ref, onMounted } from "vue"

  import { SITE_URL } from "/@/settings/siteSetting"

  import { useUserStore } from "/@/store/modules/user"
  import { useHeaderSetting } from "/@/hooks/setting/useHeaderSetting"
  import { useI18n } from "/@/hooks/web/useI18n"
  import { useDesign } from "/@/hooks/web/useDesign"
  import { useModal } from "/@/components/Modal"
  import { useMessage } from "/@/hooks/web/useMessage"
  import { useGo } from "/@/hooks/web/usePage"
  import headerImg from "/@/assets/images/header.jpg"
  import { propTypes } from "/@/utils/propTypes"
  import { openWindow } from "/@/utils"

  import { createAsyncComponent } from "/@/utils/factory/createAsyncComponent"

  import { refreshCache, queryAllDictItems } from "/@/api/infra/dict"
  import * as cache from "/@/utils/cache/module"
  import { getFileAccessHttpUrl } from "/@/utils/common/compUtils"

  type MenuEvent = "logout" | "doc" | "lock" | "cache" | "workspace" | "account" | "password"
  const { createMessage } = useMessage()

  export default defineComponent({
    name: "UserDropdown",
    components: {
      Dropdown,
      Menu,
      MenuItem: createAsyncComponent(() => import("./DropMenuItem.vue")),
      MenuDivider: Menu.Divider,
      LockAction: createAsyncComponent(() => import("../lock/LockModal.vue")),
      TenantSelect: createAsyncComponent(() => import("./TenantSelect.vue")),
      UpdatePassword: createAsyncComponent(() => import("./UpdatePassword.vue")),
    },
    props: {
      theme: propTypes.oneOf(["dark", "light"]),
    },
    setup() {
      const { prefixCls } = useDesign("header-user-dropdown")
      const { t } = useI18n()
      const { getShowDoc, getUseLockPage } = useHeaderSetting()
      const userStore = useUserStore()
      const go = useGo()

      const getUserInfo = computed(() => {
        const { userName = "", avatar, desc } = userStore.getUserInfo || {}
        return { userName, avatar: avatar || headerImg, desc }
      })

      const getAvatarUrl = computed(() => {
        let { avatar } = getUserInfo.value
        if (avatar == headerImg) {
          return avatar
        } else {
          return getFileAccessHttpUrl(avatar)
        }
      })

      const [register, { openModal }] = useModal()

      /**
       * 多部门弹窗逻辑
       */
      const loginSelectRef = ref()
      function handleLock() {
        openModal(true)
      }

      //  login out
      function handleLoginOut() {
        userStore.confirmLoginOut()
      }

      // open doc
      function openDoc() {
        openWindow(SITE_URL)
      }

      // 清除缓存
      async function clearCache() {
        const res = await queryAllDictItems()
        cache.removeDictData()
        cache.setDictItems(res.data)
        createMessage.success("刷新缓存完成！")
      }
      // 切换工作空间
      function updateWorkspace() {
        loginSelectRef.value.show()
      }

      // 修改密码
      const updatePasswordRef = ref()
      function updatePassword() {
        updatePasswordRef.value.show(userStore.getUserInfo.userName)
      }

      function handleMenuClick(e: { key: MenuEvent }) {
        switch (e.key) {
          case "logout":
            handleLoginOut()
            break
          case "doc":
            openDoc()
            break
          case "lock":
            handleLock()
            break
          case "cache":
            clearCache()
            break
          case "workspace":
            updateWorkspace()
            break
          case "password":
            updatePassword()
            break
          case "account":
            go(`/iam/userSetting`)
            break
        }
      }

      return {
        prefixCls,
        t,
        getUserInfo,
        getAvatarUrl,
        handleMenuClick,
        getShowDoc,
        register,
        getUseLockPage,
        loginSelectRef,
        updatePasswordRef,
      }
    },
  })
</script>
<style lang="less">
  @prefix-cls: ~"@{namespace}-header-user-dropdown";
  @menu-dark-subsidiary-color: rgba(255, 255, 255, 0.7);

  .@{prefix-cls} {
    height: @header-height;
    padding: 0 0 0 10px;
    padding-right: 10px;
    overflow: hidden;
    font-size: 12px;
    cursor: pointer;
    align-items: center;

    img {
      width: 24px;
      height: 24px;
      margin-right: 10px;
      background: #eee;
    }

    &__header {
      border-radius: 50%;
    }

    &__name {
      font-size: 14px;
    }

    &--dark {
      &:hover {
        background-color: @header-dark-bg-hover-color;
      }
    }

    &--light {
      &:hover {
        background-color: @header-light-bg-hover-color;
      }

      .@{prefix-cls}__name {
        color: @text-color-base;
      }

      .@{prefix-cls}__desc {
        color: @header-light-desc-color;
      }
    }

    &-dropdown-overlay {
      .ant-dropdown-menu-item {
        min-width: 160px;
      }
    }

    &-menu-subtitle {
      line-height: 13px;
      span {
        font-weight: bold;
        opacity: 0.7;
        svg {
          padding-top: 3px;
        }
      }
    }

    &-sidebar {
      .@{prefix-cls} {
        height: auto;
        cursor: default;
        padding: 8px 10px 10px 10px;

        img {
          width: 45px;
          height: 45px;
          transition: all 0.1s;
        }

        &__name {
          font-weight: bold;
        }

        &__btns {
          padding-top: 3px;
          font-size: 10px;
          white-space: nowrap;

          .anticon {
            padding-right: 2px;
          }

          .online {
            padding-right: 9px;

            .anticon {
              color: #3c763d;
            }
          }

          .logout {
            .anticon {
              color: #a94442;
            }
          }
        }

        &--dark {
          color: @menu-dark-subsidiary-color;

          a {
            color: @menu-dark-subsidiary-color;
          }

          &:hover {
            background-color: transparent;
          }
        }

        &--light {
          color: @text-color-base;

          a {
            color: @text-color-base;
          }

          &:hover {
            background-color: transparent;
          }
        }
      }
    }
  }

  .ant-layout-sider-collapsed {
    .@{prefix-cls} {
      padding: 10px 0;
      justify-content: center;

      img {
        width: 25px;
        height: 25px;
        margin: 0;
      }

      &__info {
        display: none;
      }
    }
  }
</style>
